import { getLogger } from 'log4js';
import { connect, MqttClient } from 'mqtt';

const logger = getLogger();

/**
 * 创建连接
 * @param uri mqtt连接地址
 */
export default function init(uri: string) {
	return new Promise<MqttClient>((resolve) => {
		const client = connect(uri);

		client.on('connect', () => {
			logger.debug('MQTT connected');
			resolve(client);
		});

		client.on('error', (err) => {
			logger.debug('MQTT Error: [' + err.name + ']' + err.message);
		});
	});
}
